package org.erepublik.company.service;

import java.util.ArrayList;
import java.util.Collection;

import org.erepublik.company.CountryData;
import org.erepublik.company.EAlliance;
import org.erepublik.company.ECountry;
import org.erepublik.company.EWorld;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONCountryDataProvider extends JSONProductService<CountryData>{

    public JSONCountryDataProvider() {
        super("country");
    }

    @Override
    protected CountryData[] getEmptyTable() {
        return new CountryData[0];
    }

    public CountryData getCountryData(ECountry country) {
        return productMap.get(country)[0] ;
    }
    
    public CountryData[] getAllCountryData() {
        update();
        CountryData[] data = new CountryData[ ECountry.values().length + 1 + EAlliance.values().length ];
        for (int i=0; i < ECountry.values().length; i++) {
            data[i] = productMap.get( ECountry.values()[i] )[0];
            data[i].setLocation( ECountry.values()[i] );
        }
        data[ECountry.values().length] = productMap.get( null )[0] ;
        data[ECountry.values().length].setLocation(EWorld.GLOBAL);
        
        for (int i=0; i < EAlliance.values().length; i++)
            data[i+1+ECountry.values().length] = getAllianceData(EAlliance.values()[i]);
        
        return data ;
    }
    
    
    private CountryData getAllianceData(EAlliance aliance) {
        Collection<CountryData[]> data = productMap.values();
        
        double averageCurrencyForGold=0;
        double totalCurrencyTreasury=0;
        double totalGoldTreasury=0;
        double averageGoldForCurrency=0;
        int totalNewCitizens=0;
        int totalOnline=0;
        int totalPopulation=0;
        

        for (CountryData[] countryDatas : data) {
            CountryData countryData = countryDatas[0];
            ECountry country = countryData.getCountry();
            if (country==null)
                continue;
            if (country.getAlliance() == aliance) {
                averageCurrencyForGold += countryData.getCurrencyForGold() * countryData.getPopulation();
                totalCurrencyTreasury += countryData.getCurrencyTreasury();
                totalGoldTreasury += countryData.getGoldTreasury();
                averageGoldForCurrency += countryData.getGoldForCurrency() * countryData.getPopulation();
                totalNewCitizens += countryData.getNewCitizens();
                totalOnline += countryData.getOnline();
                totalPopulation += countryData.getPopulation();
            }
        }
        averageCurrencyForGold /= totalPopulation;
        averageGoldForCurrency /= totalPopulation;
        
        CountryData allianceCountryData = new CountryData();
        allianceCountryData.setLocation(aliance);
        allianceCountryData.setCurrencyForGold(averageCurrencyForGold);
        allianceCountryData.setCurrencyTreasury(totalCurrencyTreasury);
        allianceCountryData.setGoldTreasury(totalGoldTreasury);
        allianceCountryData.setGoldForCurrency(averageGoldForCurrency);
        allianceCountryData.setNewCitizens(totalNewCitizens);
        allianceCountryData.setOnline(totalOnline);
        allianceCountryData.setPopulation(totalPopulation);
        
        return allianceCountryData;
    }

    
    @Override
    protected CountryData[] getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        return getCountryDataFromJSONArray(jsonArrayWithFood).toArray(new CountryData[0]);
    }
    
    public static ArrayList<CountryData> getCountryDataFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        ArrayList<CountryData> data = new ArrayList<CountryData>();
        for (int i = 0; i < jsonArrayWithFood.length(); i++) {
            data.add( CountryData.fromJSONObject( (JSONObject)jsonArrayWithFood.get(i) ) );
        }
        return data;
    }  
    

}
